#!/usr/bin/env python
# encoding: utf-8
"""
@author:zwb
@file: filters.py
@contact: zwb15083976291@163.com
@time: 2022/02/21
"""

# 前台文章列表查询过滤
from common.models import BlogTagArticle
from utils.filters.views_filters import SearchArticlesFilter, BaseSearchFilter


# 为 文章提供 标签过滤
class FrontEndArticlesTagFilter(SearchArticlesFilter):
    __search_field__ = 'articles_title__contains'
    # 其他过滤条件
    __other_field__ = ['categorie']

    def filter_queryset(self, request, query_set, view):
        validated_data = view.serializer.validated_data
        query_set = super().filter_queryset(request, query_set, view)
        # 查看是否存在tags
        tag = validated_data.get("tag", None)
        if tag is not None:
            articles_ids = BlogTagArticle.objects.filter(tag_id=tag).values_list('article_id', flat=True)
            query_set = query_set.filter(id__in=articles_ids)

        return query_set


class CommentsArticlesFilter(SearchArticlesFilter):
    __other_field__ = ['comments_type', 'article_id']

    def filter_queryset(self, request, query_set, view):
        query_set = super(CommentsArticlesFilter, self).filter_queryset(request, query_set, view)
        # 要求不能是二级评论
        query_set = query_set.filter(father_id='')
        return query_set


class CommentsChildrenFilter(SearchArticlesFilter):
    __other_field__ = ['comments_type', 'father_id']
